<?php include('header.php');?>
<?php
if($_GET['action'] == 'save' && $_POST['reservation_id'] != '' && $_POST['receive_rdo'] !='' && $_POST['name_receive'] != '' ){
    $time = date("Y-m-d H:i:s");
    mysql_query("INSERT INTO deposit (reservation_detail_id,deposit_type,deposit_name,deposit_phone,deposit_date) VALUES ('".$_POST['reservation_id']."','".$_POST['receive_rdo']."','".$_POST['name_receive']."','".$_POST['phone_receive']."','$time') ");

    //อัพเดทสถานะห้องเป็น จอง มัดจำแล้ว
    mysql_query("UPDATE reservation SET reservation_status = '1' WHERE reservation_detail_id = '".$_POST['reservation_id']."'");
    
    //หารหัสล่าสุดไปบันทึกข้อมูลการมัดจำ
    $Query = mysql_query("SELECT max(deposit_id) as deposit_id FROM deposit");
    $Row = mysql_fetch_array($Query);
    
    switch ($_POST['receive_rdo']) {
    case cash:
        mysql_query("INSERT INTO deposit_cash (deposit_id,deposit_cash_percent,deposit_cash_total) VALUES ('".$Row['deposit_id']."','".$_POST['cash_percent']."','".$_POST['cash_total']."')");
        break;
    
    case transfer:
        mysql_query("INSERT INTO deposit_transfer (deposit_id,bank_id,deposit_transfer_total) VALUES ('".$Row['deposit_id']."','".$_POST['bank_transfer']."','".$_POST['transfer_total']."')");
        break;
    
    case check:
        mysql_query("INSERT INTO deposit_check (deposit_id,deposit_check_number,deposit_check_bank,deposit_check_total) VALUES ('".$Row['deposit_id']."','".$_POST['check_number']."','".$_POST['check_bank']."','".$_POST['check_total']."')");
        break;
    }
    
    echo "<script>window.location.href = 'deposit_print.php?id=".$_POST['reservation_id']."'</script>";
}
    

?>
<body>
<form id="form1" name="form1" action="?action=save" method="post">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>รับเงินมัดจำ</h1>
    <div class="buttons"><a onclick="form1.submit();" class="button"><span>ยืนยันการมัดจำ</span></a><a onclick="Javascript:window.location='deposit_detail.php?id=<?php echo $_POST['reservation_id'] ?>';" class="button"><span>กลับไปแก้ไข</span></a></div>
</div>
  <div class="content">
 
<fieldset class="ui-corner" >
<legend>รับเงินมัดจำ</legend>
<input type="hidden" id="reservation_id" name="reservation_id" value="<?php echo $_POST['reservation_id']; ?>" />
   <table class="form" id="deposit_Form">
<?php 
if($_POST['receive_rdo'] == 'cash'){
?>
       <tr>
        <td><input type="hidden" id="rRdo_1" name="receive_rdo" value="cash" /> <label for="rRdo_1"> - เงินสด</label></td>
        <td> <input type="hidden" id="cash_percent" name="cash_percent" value="<?php echo $_POST['cash_percent']; ?>" /><?php if($_POST['cash_percent'] != '0'){ ?> เป็น <?php echo $_POST['cash_percent']; ?> % &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php } ?>
            รวมเป็นเงิน <input type="hidden" id="cash_total" name="cash_total" value="<?php echo $_POST['cash_total']; ?>" /><?php echo bath_format($_POST['cash_total']); ?> บาท
        </td>
      </tr>
<?php
} else if($_POST['receive_rdo'] == 'transfer'){
    $bank = explode("#", $_POST['bank_transfer']);
    
?>
       <tr>
        <td><input type="hidden" id="rRdo_2" name="receive_rdo" value="transfer" /> <label for="rRdo_2"> - เงินโอน</label></td>
        <td><input type="hidden" id="bank_transfer" name="bank_transfer" value="<?php echo $bank[0]; ?>" /><?php echo $bank[1]; ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; เป็นเงิน <input type="hidden" id="transfer_total" name="transfer_total" value="<?php echo $_POST['transfer_total']; ?>"  /><?php echo bath_format($_POST['transfer_total']); ?>  บาท</td>
      </tr>    
<?php
} else if($_POST['receive_rdo'] == 'check'){
?>
      <tr>
          <td><input type="hidden" id="rRdo_3" name="receive_rdo" value="check" /> <label for="rRdo_3"> - เช็ค</label></td>
          <td>เลขที่ <input type="hidden" id="check_number" name="check_number" size="12" value="<?php echo $_POST['check_number']; ?>" /><?php echo $_POST['check_number']; ?> &nbsp;&nbsp;&nbsp; ธนาคาร <input type="hidden" id="check_bank" name="check_bank" value="<?php echo $_POST['check_bank']; ?>" /><?php echo $_POST['check_bank']; ?> &nbsp;&nbsp;&nbsp;&nbsp; เป็นเงิน <input type="hidden" id="check_total" name="check_total" value="<?php echo $_POST['check_total']; ?>" /><?php echo bath_format($_POST['check_total']); ?> บาท</td>
      </tr>
<?php
}
?>
    </table>
</fieldset>
      
<fieldset class="ui-corner">
<legend>ชื่อผู้รับเงินมัดจำ</legend>
   <table class="form">
       <tr>
        <td>ชื่อ-นามสกุล : </td>
        <td><input type="hidden" id="name_receive" name="name_receive" value="<?php echo $_POST['name_receive']; ?>" /><?php echo $_POST['name_receive']; ?></td>
      </tr>
       <tr>
        <td>เบอร์โทรศัพท์ : </td>
        <td><input type="hidden" id="phone_receive" name="phone_receive" value="<?php echo $_POST['phone_receive']; ?>" /><?php echo $_POST['phone_receive']; ?></td>
      </tr>                              
    </table>
</fieldset>
  </div>
</div>
</form>
</body>
<?php include('footer.php');?>
